﻿<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button id="connId">连接</button>
    <button id="sendId">发送</button>
    接收到的信息：<span id="msgId"></span>
    <script>
        var ws = null;
        window.onload = function () {
            if ("WebSocket" in window) {
                console.log('浏览器支持WS');
            }
            console.log('页面加载完毕');
            var conn = document.getElementById('connId');
            var send = document.getElementById('sendId');
            var msgId = document.getElementById('msgId');
            msgId.innerHTML = "111";
            conn.onclick = function () {
                console.log('点击了连接');
                ws = new WebSocketServer("ws://192.168.56.1:9056");
                ws.onopen = function () {
                    console.log("连接成功");
                    send.disabled = false;//修改发送按钮可用状态
                }
                ws.onmessage = function (msg) {
                    console.log(msg);
                    msgId.innerHTML = msg.data;
                }
                ws.onclose = function () {
                    console.log("关闭连接");
                    send.disabled = true;
                }
                ws.onerror = function () {
                    console.log("出现错误");
                }
            }
            send.onclick = function () {
                console.log("开始发送");
                ws.send("hello i am demo.html");
            }
        }
    </script>
</body>

</html>
